<?php

namespace App\Http\Controllers\User;

use App\Http\Classes\Sentence;
use App\Http\Classes\UserLesson;
use App\Http\Classes\UserRemember;
use App\Http\Classes\Word;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PlayController extends Controller
{
    /*
     * 测试播放
     * GET
     * lesson (int)
     * type (string)
     */
    public function testPlay(Request $request)
    {
        try
        {
            if ( $request->has('lesson') && $request->has('type'))
            {
                $lesson = $request->get('lesson');
                $lessonObject = new UserLesson($lesson, $_COOKIE['USER_ID']);
                $lesson = $lessonObject->getLessonId();
                if ($lesson)
                {
                    $type = $request->get('type');
                    switch ($type)
                    {
                        case 'word':
                            $rememberWords = new UserRemember($_COOKIE['USER_ID']);
                            $rememberWords = $rememberWords->getUserAllRememberWordsArray();
                            $word = new Word();
                            $words = $word -> getWordsByLessonIdWithOutRemember($lesson, $rememberWords);
                            if ( $words )
                            {
                                return $this->response(1, '获取成功', $words);
                            }
                            else
                            {
                                return $this->response(0, '获取失败！'. $word->getMessage());
                            }
                            break;
                        case 'sentence':
                            $rememberSentences = new UserRemember($_COOKIE['USER_ID']);
                            $rememberSentences = $rememberSentences->getUserAllRememberSentencesArray();
                            $sentence = new Sentence();
                            $sentences = $sentence -> getSentenceByLessonIdWithOutRemember($lesson, $rememberSentences);
                            if ( $sentences )
                            {
                                return $this->response(1, '获取成功', $sentences);
                            }
                            else
                            {
                                return $this->response(0, '获取失败！'. $sentence->getMessage());
                            }
                            break;
                        default:
                            $rememberWords = new UserRemember($_COOKIE['USER_ID']);
                            $rememberWords = $rememberWords->getUserAllRememberWordsArray();
                            $word = new Word();
                            $words = $word -> getWordsByLessonIdWithOutRemember($lesson, $rememberWords);
                            if ( $words )
                            {
                                return $this->response(1, '获取成功', $words);
                            }
                            else
                            {
                                return $this->response(0, '获取失败！'. $word->getMessage());
                            }
                            break;
                    }
                }
                else
                {
                    return $this->response(0, '获取失败！' . $lessonObject->getMessage());
                }
            }
            else
            {
                return $this->response(0, '缺少参数');
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }
}
